package com.peas.platform.module.icims.contract.initial.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.base.approve.component.ApproveCallback;
import com.peas.platform.module.icims.base.approve.dto.ApproveDto;
import com.peas.platform.module.icims.base.approve.dto.ApproveOrderNodeDto;
import com.peas.platform.module.icims.base.approve.enums.ApproveOrderBusinessType;
import com.peas.platform.module.icims.base.approve.service.ApproveOrderService;
import com.peas.platform.module.icims.base.approve.service.ApproveService;
import com.peas.platform.module.icims.contract.initial.controller.vo.ContractInitialCheckingVo;
import com.peas.platform.module.icims.contract.initial.domain.ContractInitialCheckingDomain;
import com.peas.platform.module.icims.contract.initial.dto.ContractInitialCheckingAttachmentDto;
import com.peas.platform.module.icims.contract.initial.dto.ContractInitialCheckingDto;
import com.peas.platform.module.icims.contract.initial.dto.ContractInitialDto;
import com.peas.platform.module.icims.contract.initial.enums.ContractInitialCheckingState;
import com.peas.platform.module.icims.contract.initial.enums.ContractInitialState;
import com.peas.platform.module.icims.contract.initial.service.ContractInitialCheckingAttachmentService;
import com.peas.platform.module.icims.contract.initial.service.ContractInitialCheckingService;
import com.peas.platform.module.icims.contract.initial.service.ContractInitialProcessService;
import com.peas.platform.module.icims.contract.initial.service.ContractInitialService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ContractInitialCheckingServiceImpl extends BaseServiceImpl<ContractInitialCheckingDto> implements ContractInitialCheckingService, ApproveCallback {

    @Resource
    private ContractInitialCheckingDomain contractInitialCheckingDomain;
    @Resource
    private ContractInitialCheckingAttachmentService contractInitialCheckingAttachmentService;
    @Resource
    private ContractInitialService contractInitialService;
    @Resource
    private ContractInitialProcessService contractInitialProcessService;
    @Resource
    private ApproveService approveService;
    @Resource
    private ApproveOrderService approveOrderService;

    @Override
    public BaseDomain<ContractInitialCheckingDto, ? extends BasePo> getDomain() {
        return contractInitialCheckingDomain;
    }

    @Transactional
    @Override
    public void checkingContractInitial(ContractInitialDto dto, Long approveId, List<ApproveOrderNodeDto> approveOrderNodes, List<ContractInitialCheckingVo> items) {
        ApproveDto ad = approveService.getById(approveId);
        ApproveOrderBusinessType approveOrderBusinessType = ApproveOrderBusinessType.contractChecking;
        Assert.notNull(approveOrderBusinessType, "合同数据错误");
        items.forEach(v -> {
            Assert.notNull(v.getId(), "ID不能为空");
            ContractInitialCheckingDto item = BeanUtil.copyProperties(v, ContractInitialCheckingDto.class);
            item.setState(ContractInitialCheckingState.submit.getKey());
            this.updateById(item);
            List<ContractInitialCheckingAttachmentDto> attachments = BeanUtil.copyToList(v.getAttachments(), ContractInitialCheckingAttachmentDto.class);
            attachments.forEach(m -> m.setContractInitialCheckingId(item.getId()));
            contractInitialCheckingAttachmentService.saveBatch(attachments);
            String desc = "合同编码(" + dto.getCode() + ")";
            approveOrderService.createApproveOrder(ad, approveOrderNodes, approveOrderBusinessType, item.getId(), desc, null, this.getClass());
        });
    }

    @Transactional
    @Override
    public void revocationContractInitialChecking(Long id) {
        ContractInitialCheckingDto dto = this.getById(id);
        Assert.notNull(dto, "找不到数据");
        Assert.equals(dto.getState(), ContractInitialCheckingState.submit.getKey(), "单据状态不是[{}]", ContractInitialCheckingState.submit.getValue());
        dto.setState(ContractInitialCheckingState.revocation.getKey());
        this.updateById(dto);
        ApproveOrderBusinessType approveOrderBusinessType = ApproveOrderBusinessType.contractChecking;
        Assert.notNull(approveOrderBusinessType, "合同数据错误");
        approveOrderService.revocationApproveOrder(approveOrderBusinessType, id);
    }

    @Transactional
    @Override
    public void callbackAfterApprove(Long businessId, boolean isPass, boolean isFinalPass) {
        if (businessId == null)
            return;
        if (isPass) {
            if (isFinalPass) {
                ContractInitialCheckingDto dto = this.getById(businessId);
                dto.setState(ContractInitialCheckingState.pass.getKey());
                this.updateById(dto);
                ContractInitialDto cid = contractInitialService.getById(dto.getContractInitialId());
                cid.setState(ContractInitialState.checked.getKey());
                List<ContractInitialCheckingDto> list = this.listByField("contractInitialId", cid.getId());
                long notOver = list.stream().filter(item -> !StrUtil.equals(item.getState(), ContractInitialCheckingState.pass.getKey())).count();
                if (notOver > 0) {
                    cid.setState(ContractInitialState.checking.getKey());
                }
                cid.setState(ContractInitialState.checking.getKey());
                contractInitialService.updateById(cid);
                contractInitialProcessService.saveContractInitialProcessDto(cid, ContractInitialState.checking);
            }
        } else {
            ContractInitialCheckingDto dto = this.getById(businessId);
            dto.setState(ContractInitialCheckingState.reject.getKey());
            this.updateById(dto);
        }
    }
}
